﻿namespace WMS6.Web.OrderManagement
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Globalization;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.CommonMethodServiceRef;
    using WMS6.Web.OrderMgtServiceRef;

    public class OnLineSO2 : BasePage
    {
        protected HtmlAnchor aImportSalesOrder;
        protected HtmlInputButton btnAction;
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnSave;
        protected Button btnSubmit2;
        protected CheckBox chkPriority;
        protected DropDownList ddlClock;
        protected DropDownList ddlStatus;
        protected HtmlForm F3052;
        protected GridView gvSalesOrderDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLotAttr01RowIndex;
        protected HtmlInputHidden hdnOrderby;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnOwnerIDS;
        protected HtmlInputHidden hdnRowIndex;
        protected HtmlInputHidden hdnSkuChanged;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlHead Head1;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblActualShipDate;
        protected Label lblCustomerID;
        protected Label lblInsert;
        protected Label lblMsg;
        protected Label lblOrderDate;
        protected Label lblOrderID;
        protected Label lblRed;
        protected Label lblRedText;
        protected Label lblRemark;
        protected Label lblRequestedDeliveryDate;
        protected Label lblSalesOrder;
        protected Label lblStatus;
        protected Label lblSubmittedBy;
        protected Label lblSubmittedDate;
        protected Label lblTotalQty;
        protected Label lblYellow;
        protected Label lblYellowText;
        protected LinkButton lnkbtnImportSalesOrderDetail;
        protected LinkButton lnkbtnPrintSO;
        protected LinkButton lnkbtnSalesOrderCancel;
        protected Button lnkbtnSalesOrderSubmit;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnImportSalesOrderDetail;
        protected HtmlTableCell tdlnkbtnPrintsO;
        protected HtmlTableCell tdlnkbtnSalesOrderCancel;
        private int totalOrderQty = 0;
        protected TextBox txtActualShipDate;
        protected TextBox txtCustomerID;
        protected TextBox txtOrderDate;
        protected TextBox txtOrderID;
        protected TextBox txtRemark;
        protected TextBox txtRequestedDeliveryDate;
        protected TextBox txtSubmittedBy;
        protected TextBox txtSubmittedDate;
        protected TextBox txtTotalQty;
        protected UpdatePanel up1;

        public string BindingLotAttr01(string skuID, string lotAttr01, string invQty)
        {
            string result = string.Empty;
            try
            {
                if (!invQty.StartsWith("0"))
                {
                    result = skuID + lotAttr01;
                }
            }
            catch
            {
            }
            return result;
        }

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            DataTable salesOrderDetailDT = this.GetOnLineSOSchema();
            if (this.gvSalesOrderDetail.Rows.Count > 0)
            {
                salesOrderDetailDT.Rows.Clear();
                for (int i = 0; i < this.gvSalesOrderDetail.Rows.Count; i++)
                {
                    if (this.gvSalesOrderDetail.Rows[i].RowType == DataControlRowType.DataRow)
                    {
                        DataRow tempdr = salesOrderDetailDT.NewRow();
                        tempdr["SkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtSkuID")).Text.Trim();
                        tempdr["SkuDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblSkuDescr")).ToolTip.Trim();
                        tempdr["OriginalQty"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim();
                        tempdr["PackID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblPackID")).Text.Trim();
                        tempdr["UomID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).ToolTip.Trim();
                        tempdr["UomDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).Text.Trim();
                        tempdr["Remark"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtRemark")).Text.Trim();
                        tempdr["StatusD"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblStatus")).Text.Trim();
                        tempdr["ShippedQty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblShippedQty")).Text.Trim();
                        tempdr["Udf1"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtLotAttr01")).Text.Trim();
                        tempdr["AltSkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtAltSkuID")).Text.Trim();
                        tempdr["Qty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblInvQty")).Text.Trim();
                        salesOrderDetailDT.Rows.Add(tempdr);
                    }
                }
            }
            DataRow dr = salesOrderDetailDT.NewRow();
            salesOrderDetailDT.Rows.Add(dr);
            base.BindData2GridView(this.gvSalesOrderDetail, salesOrderDetailDT);
            ((TextBox) this.gvSalesOrderDetail.Rows[this.gvSalesOrderDetail.Rows.Count - 1].FindControl("txtSkuID")).Focus();
            ((TextBox) this.gvSalesOrderDetail.Rows[this.gvSalesOrderDetail.Rows.Count - 1].FindControl("txtSkuID")).BackColor = Color.White;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtOrderID.Text.Trim()))
            {
                try
                {
                    if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = "只能删除状态为新建的订单!";
                    }
                    else
                    {
                        List<string> orderID = new List<string> {
                            this.txtOrderID.Text.Trim()
                        };
                        this.lblMsg.Text = "订单 " + OrderMgtServiceClientProxy.GetInstance().DeleteSalesOrderInfo(orderID) + " 删除成功!";
                        this.btnSave.Enabled = false;
                        this.btnDelete.Enabled = false;
                        this.btnCopy.Enabled = false;
                        this.btnAction.Disabled = true;
                        this.btnRefresh.Enabled = false;
                        this.lnkbtnSalesOrderSubmit.Enabled = false;
                        this.lnkbtnSalesOrderCancel.Enabled = false;
                        base.DataEntityViewState = null;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "删除失败: " + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            DataRow dr;
            DataTable salesOrderDetailDT = this.GetOnLineSOSchema();
            if (this.gvSalesOrderDetail.Rows.Count > 0)
            {
                salesOrderDetailDT.Rows.Clear();
                int totalQty = 0;
                for (int i = 0; i < this.gvSalesOrderDetail.Rows.Count; i++)
                {
                    if ((this.gvSalesOrderDetail.Rows[i].RowType == DataControlRowType.DataRow) && !((CheckBox) this.gvSalesOrderDetail.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        DataRow tempdr = salesOrderDetailDT.NewRow();
                        tempdr["SkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtSkuID")).Text.Trim();
                        tempdr["SkuDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblSkuDescr")).ToolTip.Trim();
                        tempdr["OriginalQty"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim();
                        if (!string.IsNullOrEmpty(((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim()))
                        {
                            totalQty += Convert.ToInt32(((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim());
                        }
                        tempdr["PackID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblPackID")).Text.Trim();
                        tempdr["UomID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).ToolTip.Trim();
                        tempdr["UomDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).Text.Trim();
                        tempdr["Remark"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtRemark")).Text.Trim();
                        tempdr["StatusD"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblStatus")).Text.Trim();
                        tempdr["ShippedQty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblShippedQty")).Text.Trim();
                        tempdr["Udf1"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtLotAttr01")).Text.Trim();
                        tempdr["AltSkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtAltSkuID")).Text.Trim();
                        tempdr["Qty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblInvQty")).Text.Trim();
                        salesOrderDetailDT.Rows.Add(tempdr);
                    }
                }
                this.txtTotalQty.Text = totalQty.ToString();
            }
            if (salesOrderDetailDT == null)
            {
                salesOrderDetailDT = this.GetOnLineSOSchema();
                dr = salesOrderDetailDT.NewRow();
                salesOrderDetailDT.Rows.Add(dr);
            }
            else if (salesOrderDetailDT.Rows.Count == 0)
            {
                dr = salesOrderDetailDT.NewRow();
                salesOrderDetailDT.Rows.Add(dr);
            }
            base.BindData2GridView(this.gvSalesOrderDetail, salesOrderDetailDT);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("OnLineSO2Query.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("OnLineSO2List.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() == "NEW")
                {
                    if (string.IsNullOrEmpty(this.hdnOwnerID.Value))
                    {
                        this.lblMsg.Text = "请先设置默认货主!";
                    }
                    else if (!string.IsNullOrEmpty(this.txtCustomerID.Text))
                    {
                        if (!string.IsNullOrEmpty(this.hdnSkuChanged.Value))
                        {
                            this.lblMsg.Text = this.hdnSkuChanged.Value;
                        }
                        else
                        {
                            string orderID;
                            SalesOrderInfo salesOrderInfo = null;
                            if (this.Session["SalesOrder"] != null)
                            {
                                salesOrderInfo = this.Session["SalesOrder"] as SalesOrderInfo;
                            }
                            else
                            {
                                salesOrderInfo = new SalesOrderInfo();
                            }
                            salesOrderInfo = this.GetSalesOrderInfoFromPage(salesOrderInfo);
                            salesOrderInfo.UpdatedBy = base.UserInfo.UserID;
                            salesOrderInfo.UpdatedDate = DateTime.Now;
                            if (string.IsNullOrEmpty(this.txtOrderID.Text.Trim()))
                            {
                                salesOrderInfo.CreatedBy = base.UserInfo.UserID;
                                salesOrderInfo.CreatedDate = DateTime.Now;
                                salesOrderInfo.TransportationMode = "LTL";
                                orderID = OrderMgtServiceClientProxy.GetInstance().TJInsertSalesOrderInfo(salesOrderInfo);
                                this.txtOrderID.Text = orderID;
                                this.Session["SalesOrder"] = salesOrderInfo;
                                this.lblMsg.Text = "订单 " + orderID + " 保存成功!";
                            }
                            else
                            {
                                orderID = OrderMgtServiceClientProxy.GetInstance().TJUpdateOnLineSO(salesOrderInfo);
                                this.txtOrderID.Text = orderID;
                                this.lblMsg.Text = "订单 " + orderID + " 更新成功!";
                            }
                            this.SetOrderInfo2Form(this.txtOrderID.Text);
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = "请设置默认客户!";
                    }
                }
                else
                {
                    this.lblMsg.Text = "订单状态不为新建,无法保存!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void btnSubmit2_Click(object sender, EventArgs e)
        {
            this.lnkbtnSalesOrderSubmit_Click(null, null);
        }

        protected string ConvertQty(string qty)
        {
            string temp = string.Empty;
            if (string.IsNullOrEmpty(qty))
            {
                return temp;
            }
            if (qty.Contains<char>('.'))
            {
                qty = qty.Substring(0, qty.IndexOf('.'));
            }
            return int.Parse(qty).ToString();
        }

        protected void ConvertSalesOrder2ShippingOrder()
        {
            try
            {
                List<string> gInitLocal0 = new List<string> {
                    this.txtOrderID.Text
                };
                string shippingOrderID = OrderMgtServiceClientProxy.GetInstance().TJConvertSalesOrder2ShippingOrder(gInitLocal0);
                if (!string.IsNullOrEmpty(shippingOrderID.Trim()))
                {
                    this.lblMsg.Text = "订单 " + this.txtOrderID.Text.Trim() + " 提交成功!";
                    this.btnSubmit2.Enabled = false;
                    this.lnkbtnSalesOrderSubmit.Enabled = false;
                    this.lnkbtnImportSalesOrderDetail.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnAddDetail.Enabled = false;
                    this.btnDeleteDetail.Enabled = false;
                    this.ddlStatus.SelectedValue = "Scheduled";
                    List<string> mailList = new List<string> {
                        base.UserInfo.UserID,
                        this.txtOrderID.Text,
                        shippingOrderID
                    };
                    base.SandMail("OnLineSO", mailList);
                }
                else
                {
                    this.lblMsg.Text = "订单 " + this.txtOrderID.Text.Trim() + " 提交失败!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "订单 " + this.txtOrderID.Text.Trim() + " 转换失败:" + ex.Message;
            }
        }

        private DataTable GetOnLineSOSchema()
        {
            DataTable salesOrderDetailDT = new DataTable();
            salesOrderDetailDT.Columns.Add("LineID");
            salesOrderDetailDT.Columns.Add("SkuID");
            salesOrderDetailDT.Columns.Add("AltSkuID");
            salesOrderDetailDT.Columns.Add("SkuDescr");
            salesOrderDetailDT.Columns.Add("OriginalQty");
            salesOrderDetailDT.Columns.Add("PackID");
            salesOrderDetailDT.Columns.Add("UomID");
            salesOrderDetailDT.Columns.Add("UomDescr");
            salesOrderDetailDT.Columns.Add("Remark");
            salesOrderDetailDT.Columns.Add("Status");
            salesOrderDetailDT.Columns.Add("StatusD");
            salesOrderDetailDT.Columns.Add("ShippedQty");
            salesOrderDetailDT.Columns.Add("UnitPrice");
            salesOrderDetailDT.Columns.Add("ExtendedPrice");
            salesOrderDetailDT.Columns.Add("Udf1");
            salesOrderDetailDT.Columns.Add("Qty");
            return salesOrderDetailDT;
        }

        private SalesOrderInfo GetSalesOrderInfoFromPage(SalesOrderInfo salesOrderInfo)
        {
            salesOrderInfo.OrderID = this.txtOrderID.Text.Trim();
            salesOrderInfo.OwnerID = this.hdnOwnerID.Value.Trim();
            if (this.chkPriority.Checked)
            {
                salesOrderInfo.Priority = "1";
            }
            else
            {
                salesOrderInfo.Priority = "3";
            }
            salesOrderInfo.Status = this.ddlStatus.SelectedValue;
            salesOrderInfo.OrderDate = DateTime.Parse(this.txtOrderDate.Text);
            salesOrderInfo.ConsigneeID = this.txtCustomerID.Text.Trim();
            salesOrderInfo.RequestedDeliveryDate = DateTime.Parse(this.txtRequestedDeliveryDate.Text + " " + this.ddlClock.SelectedItem.Text);
            salesOrderInfo.Remark = this.txtRemark.Text.Trim();
            if (string.IsNullOrEmpty(this.txtOrderID.Text.Trim()))
            {
                CustomerInfo customerInfo = this.ViewState["CustomerInfo"] as CustomerInfo;
                salesOrderInfo.CCompany = customerInfo.Company;
                salesOrderInfo.CAddress = customerInfo.Address;
                salesOrderInfo.CCity = customerInfo.City;
                salesOrderInfo.CContact1 = customerInfo.Contact1;
                salesOrderInfo.CContact2 = customerInfo.Contact2;
                salesOrderInfo.CCountry = customerInfo.Country;
                salesOrderInfo.CCountryCode = customerInfo.CountryCode;
                salesOrderInfo.CEmail1 = customerInfo.Email1;
                salesOrderInfo.CEmail2 = customerInfo.Email2;
                salesOrderInfo.CFax1 = customerInfo.Fax1;
                salesOrderInfo.CFax2 = customerInfo.Fax2;
                salesOrderInfo.CPhone1 = customerInfo.Phone1;
                salesOrderInfo.CPhone2 = customerInfo.Phone2;
                salesOrderInfo.CProv = customerInfo.State;
                salesOrderInfo.CVat = customerInfo.Vat;
                salesOrderInfo.CZip = customerInfo.Zip;
            }
            salesOrderInfo.Type = "Online";
            salesOrderInfo.ExternOrderID = "";
            salesOrderInfo.BillToID = "";
            salesOrderInfo.Door = "";
            salesOrderInfo.Route = "";
            salesOrderInfo.Stop = "";
            salesOrderInfo.IsActive = "Y";
            salesOrderInfo.DeliveryDate = DateTime.Now;
            salesOrderInfo.DeliveryDate2 = DateTime.Now;
            salesOrderInfo.DepartureDate = DateTime.Now;
            List<SalesOrderDetailInfo> salesOrderDetailInfos = null;
            if (this.gvSalesOrderDetail.Rows.Count > 0)
            {
                salesOrderDetailInfos = new List<SalesOrderDetailInfo>();
                for (int i = 0; i < this.gvSalesOrderDetail.Rows.Count; i++)
                {
                    if ((this.gvSalesOrderDetail.Rows[i].RowType == DataControlRowType.DataRow) && !(string.IsNullOrEmpty(((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtSkuID")).Text.Trim()) || string.IsNullOrEmpty(((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim())))
                    {
                        SalesOrderDetailInfo salesOrderDetailInfo = new SalesOrderDetailInfo {
                            LineID = (i + 1) * 10,
                            OwnerID = this.hdnOwnerIDS.Value.Trim(),
                            SkuID = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtSkuID")).Text.Trim(),
                            AltSkuID = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtAltSkuID")).Text.Trim(),
                            Udf1 = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtLotAttr01")).Text.Trim(),
                            OriginalQty = decimal.Parse(((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim()),
                            PackID = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblPackID")).Text.Trim(),
                            UomID = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).ToolTip.Trim(),
                            Remark = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtRemark")).Text.Trim(),
                            Udf10 = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblInvQty")).Text.Trim(),
                            ExternOrderID = "",
                            ExternLineID = "",
                            OpenQty = 0M,
                            ShippedQty = 0M,
                            AdjustedQty = 0M,
                            Status = "New",
                            UnitPrice = 0.0,
                            Tax01 = 0.0,
                            Tax02 = 0.0,
                            ExtendedPrice = 0.0,
                            ProductWeight = 0.0,
                            ProductCube = 0.0,
                            IsSubstitute = 0,
                            OkToSubstitute = 0
                        };
                        salesOrderDetailInfos.Add(salesOrderDetailInfo);
                    }
                }
            }
            salesOrderInfo.SalesOrderDetailInfos = salesOrderDetailInfos;
            return salesOrderInfo;
        }

        public List<UomList> GetUomListInfoByPackID(string packID)
        {
            List<UomList> uomList = null;
            Hashtable ht = this.ViewState["UomList"] as Hashtable;
            if ((ht != null) && (ht.Count > 0))
            {
                if (ht.Contains(packID))
                {
                    return (ht[packID] as List<UomList>);
                }
                uomList = CommoneMethodServiceClientProxy.GetInstance().GetUomListByPackID(packID);
                if ((uomList != null) && (uomList.Count > 0))
                {
                    ht.Add(packID, uomList);
                    this.ViewState["UomList"] = ht;
                }
                return uomList;
            }
            uomList = CommoneMethodServiceClientProxy.GetInstance().GetUomListByPackID(packID);
            if ((uomList != null) && (uomList.Count > 0))
            {
                ht = new Hashtable();
                ht.Add(packID, uomList);
                this.ViewState["UomList"] = ht;
            }
            return uomList;
        }

        protected void gvSalesOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
                ((System.Web.UI.WebControls.Image) e.Row.FindControl("imgSkuID")).Attributes.Add("onclick", string.Concat(new object[] { "selectSku(", ((TextBox) e.Row.FindControl("txtSkuID")).ClientID, ",", e.Row.RowIndex, ")" }));
                ((TextBox) e.Row.FindControl("txtSkuID")).Attributes.Add("onblur", "setSkuIDRowIndex(" + e.Row.RowIndex + ")");
                ((System.Web.UI.WebControls.Image) e.Row.FindControl("imgAlternateSku")).Attributes.Add("onclick", string.Concat(new object[] { "selectAlternateSku(", ((TextBox) e.Row.FindControl("txtAltSkuID")).ClientID, ",", e.Row.RowIndex, ")" }));
                ((TextBox) e.Row.FindControl("txtAltSkuID")).Attributes.Add("onblur", "setSkuIDRowIndex(" + e.Row.RowIndex + ")");
                ((TextBox) e.Row.FindControl("txtLotAttr01")).Attributes.Add("onblur", "setLotAttr01RowIndex(" + e.Row.RowIndex + ")");
                string strOriginalQty = "0";
                string strinvQty = ((Label) e.Row.FindControl("lblInvQty")).Text;
                if (!string.IsNullOrEmpty(strinvQty))
                {
                    ((TextBox) e.Row.FindControl("txtOriginalQty")).Attributes.Add("onblur", "SetCountQty(this);CountQty(this," + strinvQty + ")");
                }
                if (!string.IsNullOrEmpty(((TextBox) e.Row.FindControl("txtOriginalQty")).Text))
                {
                    strOriginalQty = ((TextBox) e.Row.FindControl("txtOriginalQty")).Text;
                    this.totalOrderQty += int.Parse(strOriginalQty);
                }
                ((TextBox) e.Row.FindControl("txtOriginalQty")).Attributes.Add("key", strOriginalQty);
                ((TextBox) e.Row.FindControl("txtSkuID")).BackColor = Color.White;
                if ((!string.IsNullOrEmpty(strOriginalQty) && !string.IsNullOrEmpty(strinvQty)) && (decimal.Parse(strOriginalQty) > decimal.Parse(strinvQty)))
                {
                    ((TextBox) e.Row.FindControl("txtOriginalQty")).BackColor = Color.Yellow;
                }
                if (string.IsNullOrEmpty(((TextBox) e.Row.FindControl("txtSkuID")).Text.Trim()))
                {
                    ((TextBox) e.Row.FindControl("txtSkuID")).BackColor = Color.Red;
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.txtTotalQty.Text = base.GetPreferenceDigit(this.totalOrderQty.ToString());
            }
        }

        protected void gvSalesOrderDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                int i;
                string strorderBy = e.SortExpression;
                if (this.hdnOrderby.Value == "ASC")
                {
                    this.hdnOrderby.Value = "DESC";
                    strorderBy = strorderBy + " DESC";
                }
                else if (this.hdnOrderby.Value == "DESC")
                {
                    this.hdnOrderby.Value = "ASC";
                    strorderBy = strorderBy + " ASC";
                }
                DataTable salesOrderDetailDT = this.GetOnLineSOSchema();
                if (this.gvSalesOrderDetail.Rows.Count > 0)
                {
                    salesOrderDetailDT.Rows.Clear();
                    for (i = 0; i < this.gvSalesOrderDetail.Rows.Count; i++)
                    {
                        if (this.gvSalesOrderDetail.Rows[i].RowType == DataControlRowType.DataRow)
                        {
                            DataRow tempdr = salesOrderDetailDT.NewRow();
                            tempdr["SkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtSkuID")).Text.Trim();
                            tempdr["AltSkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtAltSkuID")).Text.Trim();
                            tempdr["SkuDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblSkuDescr")).ToolTip.Trim();
                            tempdr["OriginalQty"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim();
                            tempdr["PackID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblPackID")).Text.Trim();
                            tempdr["UomID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).ToolTip.Trim();
                            tempdr["UomDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).Text.Trim();
                            tempdr["Remark"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtRemark")).Text.Trim();
                            tempdr["StatusD"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblStatus")).Text.Trim();
                            tempdr["ShippedQty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblShippedQty")).Text.Trim();
                            tempdr["Udf1"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtLotAttr01")).Text.Trim();
                            tempdr["Qty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblInvQty")).Text.Trim();
                            salesOrderDetailDT.Rows.Add(tempdr);
                        }
                    }
                }
                DataTable dt = salesOrderDetailDT.Clone();
                DataRow[] drs = salesOrderDetailDT.Select("", strorderBy);
                for (i = 0; drs.Count<DataRow>() > i; i++)
                {
                    dt.Rows.Add(drs[i].ItemArray);
                }
                base.BindData2GridView(this.gvSalesOrderDetail, dt);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void ImportSalesOrderData(object sender, EventArgs e)
        {
            SalesOrderInfo orderInfo = this.Session["SessionsalesOrderInfo"] as SalesOrderInfo;
            if ((orderInfo != null) && ((orderInfo.SalesOrderDetailInfos != null) && (orderInfo.SalesOrderDetailInfos.Count > 0)))
            {
                decimal CountQty = 0M;
                foreach (SalesOrderDetailInfo info in orderInfo.SalesOrderDetailInfos)
                {
                    DataRow[] dr = null;
                    CountQty += info.OriginalQty;
                    DataSet ds = null;
                    if (this.Session["InvQty"] != null)
                    {
                        ds = this.Session["InvQty"] as DataSet;
                    }
                    else if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                    {
                        ds = CommoneMethodServiceClientProxy.GetInstance().GetInvSkuQtyByOwnerID(this.hdnOwnerIDS.Value);
                        if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[0].Rows.Count > 0))
                        {
                            this.Session["InvQty"] = ds;
                        }
                    }
                    if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[0].Rows.Count > 0))
                    {
                        if (string.IsNullOrEmpty(info.Udf1))
                        {
                            dr = ds.Tables[0].Select("SKU_ID='" + info.SkuID + "'");
                        }
                        else
                        {
                            dr = ds.Tables[0].Select("SKU_ID='" + info.SkuID + "' AND LOT_ATTR01 = '" + info.Udf1 + "'");
                        }
                        if ((dr != null) && (dr.Count<DataRow>() > 0))
                        {
                            for (int i = 0; i < dr.Length; i++)
                            {
                                info.Qty += Convert.ToDecimal(dr[i]["AVAILABLE_QTY"]);
                            }
                        }
                    }
                }
                this.txtTotalQty.Text = base.GetPreferenceDigit(CountQty.ToString());
                base.BindData2GridView(this.gvSalesOrderDetail, orderInfo.SalesOrderDetailInfos);
            }
            this.Session["SessionsalesOrderInfo"] = null;
            this.Session["SalesOrder"] = orderInfo;
        }

        protected void lnkbtnSalesOrderCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() == "SCHEDULED")
                {
                    string shippingOrderID = OrderMgtServiceClientProxy.GetInstance().SalesOrderCancel(this.txtOrderID.Text);
                    this.lblMsg.Text = "订单 " + this.txtOrderID.Text.Trim() + " 取消成功!";
                    this.SetOrderInfo2Form(this.txtOrderID.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = "取消失败:单据状态不为提交,无法取消!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "取消失败:" + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnSalesOrderSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() == "NEW")
                {
                    string orderID;
                    SalesOrderInfo salesOrderInfo = null;
                    if (this.Session["SalesOrder"] != null)
                    {
                        salesOrderInfo = this.Session["SalesOrder"] as SalesOrderInfo;
                    }
                    else
                    {
                        salesOrderInfo = new SalesOrderInfo();
                    }
                    salesOrderInfo = this.GetSalesOrderInfoFromPage(salesOrderInfo);
                    salesOrderInfo.UpdatedBy = base.UserInfo.UserID;
                    salesOrderInfo.UpdatedDate = DateTime.Now;
                    if (string.IsNullOrEmpty(this.txtOrderID.Text.Trim()))
                    {
                        salesOrderInfo.CreatedBy = base.UserInfo.UserID;
                        salesOrderInfo.CreatedDate = DateTime.Now;
                        orderID = OrderMgtServiceClientProxy.GetInstance().TJInsertSalesOrderInfo(salesOrderInfo);
                        this.txtOrderID.Text = orderID;
                    }
                    else
                    {
                        orderID = OrderMgtServiceClientProxy.GetInstance().TJUpdateOnLineSO(salesOrderInfo);
                        this.txtOrderID.Text = orderID;
                    }
                    this.ConvertSalesOrder2ShippingOrder();
                }
                else
                {
                    this.lblMsg.Text = "提交失败:单据状态不为新建,无法提交!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            this.txtOrderDate.Enabled = false;
            this.txtActualShipDate.Enabled = false;
            base.BindData2DDLBySysCode(this.ddlStatus, "OrderStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnOrderID.Value = base.Request.QueryString["ID"].ToString();
                string orderID = this.hdnOrderID.Value.ToString();
                this.txtOrderID.Text = orderID;
                this.SetOrderInfo2Form(orderID);
            }
            else
            {
                int i;
                this.hdnOwnerID.Value = base.UserInfo.OwnerID;
                List<WMS6.Web.CommonMethodServiceRef.OwnerInfo> ownerList = CommoneMethodServiceClientProxy.GetInstance().GetAuthorizedOwnerByUserID();
                if ((ownerList != null) && (ownerList.Count > 0))
                {
                    string strOwnerID = string.Empty;
                    for (i = 0; ownerList.Count > i; i++)
                    {
                        strOwnerID = strOwnerID + ownerList[i].OwnerID + ",";
                    }
                    strOwnerID = strOwnerID.Substring(0, strOwnerID.Length - 1);
                    this.hdnOwnerIDS.Value = strOwnerID;
                }
                if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                {
                    DataSet ds = CommoneMethodServiceClientProxy.GetInstance().GetInvSkuQtyByOwnerID(this.hdnOwnerIDS.Value);
                    if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[0].Rows.Count > 0))
                    {
                        this.Session["InvQty"] = ds;
                    }
                }
                base.SetDefaultCustomer(this.txtCustomerID);
                this.txtCustomerID_TextChanged(null, null);
                this.txtOrderDate.Text = base.GetPreferenceShortDate(DateTime.Now);
                this.txtRequestedDeliveryDate.Text = DateTime.Now.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
                this.ddlClock.SelectedValue = (DateTime.Now.Hour + 1).ToString();
                DataTable salesOrderDetailDT = this.GetOnLineSOSchema();
                for (i = 0; i < 40; i++)
                {
                    DataRow dr = salesOrderDetailDT.NewRow();
                    salesOrderDetailDT.Rows.Add(dr);
                }
                base.BindData2GridView(this.gvSalesOrderDetail, salesOrderDetailDT);
                ((TextBox) this.gvSalesOrderDetail.Rows[0].FindControl("txtSkuID")).Focus();
                ((TextBox) this.gvSalesOrderDetail.Rows[0].FindControl("txtSkuID")).BackColor = Color.White;
            }
            this.SetButtonEnableByStatus(this.ddlStatus.SelectedValue);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
                this.hdnDataFormat.Value = "yyyy-MM-dd";
            }
            this.lblMsg.Text = "";
        }

        private void SetButtonEnableByStatus(string status)
        {
            string CS40000 = status.ToUpper();
            if (CS40000 != null)
            {
                if (!(CS40000 == "CANCELLED"))
                {
                    if (CS40000 == "SCHEDULED")
                    {
                        this.btnCopy.Enabled = false;
                        this.btnRefresh.Enabled = true;
                        this.lnkbtnSalesOrderSubmit.Enabled = false;
                        this.lnkbtnSalesOrderCancel.Enabled = true;
                    }
                }
                else
                {
                    this.btnCopy.Enabled = false;
                    this.btnRefresh.Enabled = false;
                    this.lnkbtnSalesOrderSubmit.Enabled = false;
                    this.lnkbtnSalesOrderCancel.Enabled = false;
                }
            }
            if (status.ToUpper() != "NEW")
            {
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
                this.btnAddDetail.Enabled = false;
                this.btnDeleteDetail.Enabled = false;
            }
        }

        private void SetGridviewRowSkuInfo(string skuID, int rowIndex)
        {
            DataRow[] dr = null;
            List<UomList> uomList;
            WMS6.Web.CommonMethodServiceRef.SkuInfo skuInfo;
            decimal totalAvailableQty = 0M;
            if (this.Session["InvQty"] != null)
            {
                DataSet ds = this.Session["InvQty"] as DataSet;
                string vNo = ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtLotAttr01")).Text.Trim();
                if (!string.IsNullOrEmpty(vNo))
                {
                    dr = ds.Tables[0].Select("SKU_ID='" + skuID + "' AND LOT_ATTR01 = '" + vNo + "'");
                }
                else
                {
                    dr = ds.Tables[0].Select("SKU_ID='" + skuID + "'");
                }
                if ((dr != null) && (dr.Length > 0))
                {
                    for (int i = 0; i < dr.Length; i++)
                    {
                        totalAvailableQty += decimal.Parse(string.IsNullOrEmpty(dr[i]["AVAILABLE_QTY"].ToString()) ? "0" : dr[i]["AVAILABLE_QTY"].ToString());
                    }
                }
            }
            DataTable dt = this.Session["SkuList"] as DataTable;
            if ((dt != null) && (dt.Rows.Count > 0))
            {
                DataRow[] drs = dt.Select("OWNER_ID = '" + this.hdnOwnerID.Value + "' AND SKU_ID = '" + skuID + "'");
                if ((drs != null) && (drs.Length > 0))
                {
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text = drs[0]["SKU_ID"].ToString();
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).Text = base.SubString4SetLongText(drs[0]["DESCR"].ToString(), 14);
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).ToolTip = drs[0]["DESCR"].ToString();
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblStatus")).Text = "新建";
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtLotAttr01")).Focus();
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.White;
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblPackID")).Text = drs[0]["PACK_ID"].ToString();
                    uomList = this.GetUomListInfoByPackID(drs[0]["PACK_ID"].ToString());
                    if ((uomList != null) && (uomList.Count > 0))
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblUomID")).Text = uomList[0].UomDescr;
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblUomID")).ToolTip = uomList[0].UomID;
                    }
                    if ((dr != null) && (dr.Count<DataRow>() > 0))
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                    }
                    else
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit("0");
                    }
                    this.hdnSkuChanged.Value = "";
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtOriginalQty")).Attributes.Add("onblur", "SetCountQty(this);CountQty(this," + ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text + ")");
                }
                else
                {
                    skuInfo = CommoneMethodServiceClientProxy.GetInstance().GetSkuInfoByUserOwner(skuID, base.UserInfo.UserID);
                    if (((skuInfo != null) && !string.IsNullOrEmpty(skuInfo.SkuID)) && !string.IsNullOrEmpty(skuInfo.OwnerID))
                    {
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text = skuInfo.SkuID;
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).Text = base.SubString4SetLongText(skuInfo.Descr, 14);
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).ToolTip = skuInfo.Descr;
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblStatus")).Text = "新建";
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtLotAttr01")).Focus();
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.White;
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblPackID")).Text = skuInfo.PackID;
                        uomList = this.GetUomListInfoByPackID(skuInfo.PackID);
                        if ((uomList != null) && (uomList.Count > 0))
                        {
                            ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblUomID")).Text = uomList[0].UomDescr;
                            ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblUomID")).ToolTip = uomList[0].UomID;
                        }
                        if ((dr != null) && (dr.Count<DataRow>() > 0))
                        {
                            ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                        }
                        else
                        {
                            ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit("0");
                        }
                        this.hdnSkuChanged.Value = "";
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtOriginalQty")).Attributes.Add("onblur", "SetCountQty(this);CountQty(this," + ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text + ")");
                    }
                    else
                    {
                        this.hdnSkuChanged.Value = string.Concat(new object[] { "行号", rowIndex + 1, " '", skuID, "'", base.GetGlobalMsgResource("F7600_013") });
                        this.lblMsg.Text = this.hdnSkuChanged.Value;
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).Text = "";
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text = "";
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Focus();
                        ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.Red;
                    }
                }
            }
            else
            {
                skuInfo = CommoneMethodServiceClientProxy.GetInstance().GetSkuInfoByUserOwner(skuID, base.UserInfo.UserID);
                if ((((skuInfo != null) && !string.IsNullOrEmpty(skuInfo.SkuID)) && !string.IsNullOrEmpty(skuInfo.OwnerID)) && (skuInfo.IsActive == "Y"))
                {
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text = skuInfo.SkuID;
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).Text = base.SubString4SetLongText(skuInfo.Descr, 14);
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).ToolTip = skuInfo.Descr;
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblStatus")).Text = "新建";
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtLotAttr01")).Focus();
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.White;
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblPackID")).Text = skuInfo.PackID;
                    uomList = this.GetUomListInfoByPackID(skuInfo.PackID);
                    if ((uomList != null) && (uomList.Count > 0))
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblUomID")).Text = uomList[0].UomDescr;
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblUomID")).ToolTip = uomList[0].UomID;
                    }
                    if ((dr != null) && (dr.Count<DataRow>() > 0))
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                    }
                    else
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit("0");
                    }
                    this.hdnSkuChanged.Value = "";
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtOriginalQty")).Attributes.Add("onblur", "SetCountQty(this);CountQty(this," + ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text + ")");
                }
                else
                {
                    this.hdnSkuChanged.Value = string.Concat(new object[] { "行号", rowIndex + 1, " '", skuID, "'", base.GetGlobalMsgResource("F7600_013"), " 或者该货品未被启用!" });
                    this.lblMsg.Text = this.hdnSkuChanged.Value;
                    ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblSkuDescr")).Text = "";
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text = "";
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.Red;
                    ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Focus();
                }
            }
        }

        private void SetOrderInfo2Form(string orderID)
        {
            SalesOrderInfo orderInfo = OrderMgtServiceClientProxy.GetInstance().GetSalesOrderInfoByID(orderID);
            if ((orderInfo != null) && !string.IsNullOrEmpty(orderInfo.OrderID))
            {
                this.ddlStatus.SelectedValue = orderInfo.Status;
                this.txtOrderDate.Text = base.GetPreferenceShortDate(orderInfo.OrderDate);
                this.txtCustomerID.Text = orderInfo.ConsigneeID;
                this.txtRequestedDeliveryDate.Text = orderInfo.RequestedDeliveryDate.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
                this.ddlClock.SelectedValue = orderInfo.RequestedDeliveryDate.Hour.ToString();
                this.txtRemark.Text = orderInfo.Remark;
                this.txtActualShipDate.Text = base.GetPreferenceShortDate(orderInfo.ActualShipDate);
                this.txtSubmittedBy.Text = orderInfo.SubmittedBy;
                this.txtSubmittedDate.Text = base.GetPreferenceShortDate(orderInfo.SubmittedDate);
                this.hdnOwnerID.Value = orderInfo.OwnerID;
                if (orderInfo.Priority == "1")
                {
                    this.chkPriority.Checked = true;
                }
                if ((orderInfo.SalesOrderDetailInfos != null) && (orderInfo.SalesOrderDetailInfos.Count > 0))
                {
                    this.hdnOwnerIDS.Value = orderInfo.SalesOrderDetailInfos[0].OwnerID;
                    if (!string.IsNullOrEmpty(this.hdnOwnerIDS.Value))
                    {
                        DataSet ds = CommoneMethodServiceClientProxy.GetInstance().GetInvSkuQtyByOwnerID(this.hdnOwnerIDS.Value);
                        if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[0].Rows.Count > 0))
                        {
                            this.Session["InvQty"] = ds;
                        }
                    }
                    decimal CountQty = 0M;
                    foreach (SalesOrderDetailInfo info in orderInfo.SalesOrderDetailInfos)
                    {
                        CountQty += info.OriginalQty;
                        info.Qty = decimal.Parse(string.IsNullOrEmpty(info.Udf10) ? "0" : info.Udf10);
                    }
                    this.txtTotalQty.Text = base.GetPreferenceDigit(CountQty.ToString());
                    base.BindData2GridView(this.gvSalesOrderDetail, orderInfo.SalesOrderDetailInfos);
                    this.hdnOwnerIDS.Value = orderInfo.SalesOrderDetailInfos[0].OwnerID;
                }
                else
                {
                    List<WMS6.Web.CommonMethodServiceRef.OwnerInfo> ownerList = CommoneMethodServiceClientProxy.GetInstance().GetAuthorizedOwnerByUserID();
                    if ((ownerList != null) && (ownerList.Count > 0))
                    {
                        string strOwnerID = string.Empty;
                        for (int i = 0; ownerList.Count > i; i++)
                        {
                            strOwnerID = strOwnerID + ownerList[i].OwnerID + ",";
                        }
                        strOwnerID = strOwnerID.Substring(0, strOwnerID.Length - 1);
                        this.hdnOwnerIDS.Value = strOwnerID;
                    }
                }
                this.Session["SalesOrder"] = orderInfo;
            }
        }

        protected void txtAlternateSku_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdnOwnerID.Value))
            {
                this.lblMsg.Text = "请先设置默认货主!";
            }
            else
            {
                int rowIndex = int.Parse(this.hdnRowIndex.Value);
                string strAltSkuID = ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtAltSkuID")).Text.Trim();
                WMS6.Web.BaseInfoDetailServiceRef.AlternateSkuInfo alternateSku = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnOwnerID.Value, "", strAltSkuID, "ALTERNATE_SKU") as WMS6.Web.BaseInfoDetailServiceRef.AlternateSkuInfo;
                if (alternateSku != null)
                {
                    this.hdnSkuID.Value = alternateSku.SkuID;
                    this.txtSkuID_TextChanged(null, null);
                }
                else
                {
                    this.lblMsg.Text = "未找到匹配的货品别名!";
                }
            }
        }

        protected void txtCustomerID_TextChanged(object sender, EventArgs e)
        {
            CustomerInfo tradePartnerInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.txtCustomerID.Text.Trim(), "CUSTOMER") as CustomerInfo;
            if (!((tradePartnerInfo == null) || string.IsNullOrEmpty(tradePartnerInfo.CustomerID)))
            {
                this.ViewState["CustomerInfo"] = tradePartnerInfo;
            }
            else
            {
                this.txtCustomerID.Text = "";
            }
            this.txtCustomerID.Focus();
        }

        protected void txtLotAttr01_TextChanged(object sender, EventArgs e)
        {
            int rowIndex = 0;
            if (!string.IsNullOrEmpty(this.hdnLotAttr01RowIndex.Value))
            {
                rowIndex = int.Parse(this.hdnLotAttr01RowIndex.Value);
            }
            try
            {
                DataSet ds;
                DataRow[] dr;
                decimal totalAvailableQty;
                int i;
                string skuID = ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text.Trim();
                string versionNo = ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtLotAttr01")).Text.Trim();
                if (!string.IsNullOrEmpty(versionNo))
                {
                    if (!string.IsNullOrEmpty(skuID))
                    {
                        if (this.Session["InvQty"] != null)
                        {
                            ds = this.Session["InvQty"] as DataSet;
                            dr = ds.Tables[0].Select("SKU_ID='" + skuID + "' AND LOT_ATTR01 = '" + versionNo + "'");
                            totalAvailableQty = 0M;
                            if ((dr != null) && (dr.Length > 0))
                            {
                                for (i = 0; i < dr.Length; i++)
                                {
                                    totalAvailableQty += decimal.Parse(string.IsNullOrEmpty(dr[i]["AVAILABLE_QTY"].ToString()) ? "0" : dr[i]["AVAILABLE_QTY"].ToString());
                                }
                                ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                                ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblContrastSkuID")).Text = skuID + versionNo;
                            }
                            else
                            {
                                ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                                ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblContrastSkuID")).Text = "";
                            }
                        }
                    }
                    else
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit("0");
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblContrastSkuID")).Text = "";
                    }
                }
                else if (!string.IsNullOrEmpty(skuID) && (this.Session["InvQty"] != null))
                {
                    ds = this.Session["InvQty"] as DataSet;
                    dr = ds.Tables[0].Select("SKU_ID='" + skuID + "'");
                    totalAvailableQty = 0M;
                    if ((dr != null) && (dr.Length > 0))
                    {
                        for (i = 0; i < dr.Length; i++)
                        {
                            totalAvailableQty += decimal.Parse(string.IsNullOrEmpty(dr[i]["AVAILABLE_QTY"].ToString()) ? "0" : dr[i]["AVAILABLE_QTY"].ToString());
                        }
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblContrastSkuID")).Text = skuID + versionNo;
                    }
                    else
                    {
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblInvQty")).Text = base.GetPreferenceDigit(totalAvailableQty.ToString());
                        ((Label) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("lblContrastSkuID")).Text = "";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtOriginalQty")).Focus();
            }
        }

        protected void txtSkuID_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdnOwnerID.Value))
            {
                this.lblMsg.Text = "请先设置默认货主!";
            }
            else
            {
                int rowIndex = 0;
                if (!string.IsNullOrEmpty(this.hdnRowIndex.Value))
                {
                    rowIndex = int.Parse(this.hdnRowIndex.Value);
                }
                string skuID = ((TextBox) this.gvSalesOrderDetail.Rows[rowIndex].FindControl("txtSkuID")).Text.Trim();
                if (string.IsNullOrEmpty(this.hdnSkuID.Value))
                {
                    this.hdnSkuID.Value = skuID;
                }
                else
                {
                    skuID = this.hdnSkuID.Value;
                }
                string selectSkuID = this.hdnSkuID.Value;
                if (!string.IsNullOrEmpty(skuID) || !string.IsNullOrEmpty(selectSkuID))
                {
                    if (selectSkuID.Contains<char>(','))
                    {
                        int i;
                        DataRow tempdr;
                        List<string> skuIDList = new List<string>();
                        skuIDList = selectSkuID.Split(new char[] { ',' }).ToList<string>();
                        this.SetGridviewRowSkuInfo(skuIDList[0], rowIndex);
                        DataTable salesOrderDetailDT = this.GetOnLineSOSchema();
                        for (i = 0; i < this.gvSalesOrderDetail.Rows.Count; i++)
                        {
                            if (this.gvSalesOrderDetail.Rows[i].RowType == DataControlRowType.DataRow)
                            {
                                tempdr = salesOrderDetailDT.NewRow();
                                tempdr["SkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtSkuID")).Text.Trim();
                                tempdr["AltSkuID"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtAltSkuID")).Text.Trim();
                                tempdr["SkuDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblSkuDescr")).ToolTip.Trim();
                                tempdr["OriginalQty"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtOriginalQty")).Text.Trim();
                                tempdr["PackID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblPackID")).Text.Trim();
                                tempdr["UomID"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).ToolTip.Trim();
                                tempdr["UomDescr"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblUomID")).Text.Trim();
                                tempdr["Remark"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtRemark")).Text.Trim();
                                tempdr["StatusD"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblStatus")).Text.Trim();
                                tempdr["ShippedQty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblShippedQty")).Text.Trim();
                                tempdr["Udf1"] = ((TextBox) this.gvSalesOrderDetail.Rows[i].FindControl("txtLotAttr01")).Text.Trim();
                                tempdr["Qty"] = ((Label) this.gvSalesOrderDetail.Rows[i].FindControl("lblInvQty")).Text.Trim();
                                if (!string.IsNullOrEmpty(tempdr["SkuID"].ToString()))
                                {
                                    salesOrderDetailDT.Rows.Add(tempdr);
                                }
                            }
                        }
                        DataTable dt = this.Session["SkuList"] as DataTable;
                        for (i = 1; i < skuIDList.Count; i++)
                        {
                            tempdr = salesOrderDetailDT.NewRow();
                            DataRow[] dr = null;
                            if (this.Session["InvQty"] != null)
                            {
                                DataSet ds = this.Session["InvQty"] as DataSet;
                                dr = ds.Tables[0].Select("SKU_ID='" + skuIDList[i] + "'");
                            }
                            if ((dt != null) && (dt.Rows.Count > 0))
                            {
                                DataRow[] drs = dt.Select("OWNER_ID = '" + this.hdnOwnerID.Value + "' AND SKU_ID = '" + skuIDList[i] + "'");
                                if ((drs != null) && (drs.Length > 0))
                                {
                                    tempdr["SkuDescr"] = drs[0]["DESCR"].ToString();
                                    tempdr["PackID"] = drs[0]["PACK_ID"].ToString();
                                }
                            }
                            else
                            {
                                WMS6.Web.CommonMethodServiceRef.SkuInfo skuInfo = CommoneMethodServiceClientProxy.GetInstance().GetSkuInfoByUserOwner(skuIDList[i], base.UserInfo.UserID);
                                if ((((skuInfo != null) && !string.IsNullOrEmpty(skuInfo.SkuID)) && !string.IsNullOrEmpty(skuInfo.OwnerID)) && (skuInfo.IsActive == "Y"))
                                {
                                    tempdr["SkuDescr"] = skuInfo.Descr;
                                    tempdr["PackID"] = skuInfo.PackID;
                                }
                                else
                                {
                                    this.lblMsg.Text = "没有符合条件的货品记录!";
                                    base.BindData2GridView(this.gvSalesOrderDetail, salesOrderDetailDT);
                                    return;
                                }
                            }
                            tempdr["SkuID"] = skuIDList[i];
                            tempdr["StatusD"] = "新建";
                            tempdr["OriginalQty"] = "";
                            List<UomList> uomList = CommoneMethodServiceClientProxy.GetInstance().GetUomListByPackID(tempdr["PackID"].ToString());
                            if ((uomList != null) && (uomList.Count > 0))
                            {
                                tempdr["UomID"] = uomList[0].UomID;
                                tempdr["UomDescr"] = uomList[0].UomDescr;
                            }
                            tempdr["Remark"] = "";
                            if ((dr != null) && (dr.Count<DataRow>() > 0))
                            {
                                tempdr["Qty"] = base.GetPreferenceDigit(dr[0]["AVAILABLE_QTY"].ToString());
                            }
                            else
                            {
                                tempdr["Qty"] = base.GetPreferenceDigit("0");
                            }
                            salesOrderDetailDT.Rows.Add(tempdr);
                        }
                        base.BindData2GridView(this.gvSalesOrderDetail, salesOrderDetailDT);
                    }
                    else
                    {
                        this.SetGridviewRowSkuInfo(skuID, rowIndex);
                    }
                }
                this.hdnSkuID.Value = "";
            }
        }
    }
}

